package git.determinal.template.util;



import cn.hutool.http.ContentType;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;

/**
 * @Description
 * @Author 李家磊
 * @Date 2024/3/13 17:46
 */
@Component
public class FileUtils {

    /**
     * @Description 本地文件生成下载源
     * @Param response http相应
     * @Param resource 文件resource
     * @return
     **/
    public static final void downloadFile(HttpServletResponse response, Resource resource) {
        FileInputStream fileInputStream = null;
        ServletOutputStream servletOutputStream = null;
        try{
            String filename = resource.getFilename();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("octets/stream");
            // 针对不同的浏览器需要有不同的编码，否则文件名会出现乱码
            filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
            response.setHeader("Content-disposition", "attachment;filename=" + filename);

            fileInputStream = new FileInputStream(resource.getFile());
            servletOutputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                servletOutputStream.write(buffer, 0, bytesRead);
            }
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (servletOutputStream != null) {
                try {
                    servletOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * @Description 文件流生成下载源
     * @Param fileOutPutStream 文件输出流
     * @Param response 响应头
     * @return
     **/
    public static final void outputFile(FileOutputStream fileOutputStream, HttpServletResponse response) {

    }

    public static final File uploadFile(String url){
        return null;
    }

    /**
     * @Description File转MultipartFile
     * @Param file 文件
     * @return 转换后的MultipartFile
     **/
    public static MultipartFile file2MultipartFile(File file) {
        MultipartFile multipartFile;
        try {
            DiskFileItem fileItem2 = (DiskFileItem) new DiskFileItemFactory().createItem("file", ContentType.MULTIPART.getValue(), true, file.getName());
//也可以用IOUtils.copy(inputStream,os);
            Files.copy(Paths.get(file.getAbsolutePath()), fileItem2.getOutputStream());
            multipartFile = new CommonsMultipartFile(fileItem2);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return multipartFile;
    }

    /**
     * @Description MultipartFile转File
     * @Param path 文件的路径
     * @Param multipartFile 文件
     * @return 转换后的File
     **/
    public static File multipartFile2File(String path, MultipartFile multipartFile) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileCopyUtils.copy(multipartFile.getBytes(), file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}



